File manager - Edit - /home/autoph/public_html/projects/app/Http/Controllers/API/v1/NotificationController.php
Back
<?php namespace App\Http\Controllers\API\v1; use App\Http\Controllers\Controller; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Throwable; class NotificationController extends Controller { public function index(Request $request) { $user = Auth::user(); // DB::enableQueryLog(); $notificationsQuery = DB::table('notifications') ->join('users', 'notifications.notifiable_id', '=', 'users.id'); // if ($user->employee_id != 1) { // $notificationsQuery->where('notifications.notifiable_id', '=', $user->id); // } $notifications = $notificationsQuery->select('notifications.*', 'users.name') ->paginate(10); // Here 10 is the number of items per page, you can adjust it // Return paginated data as JSON return response()->json($notifications); } public function show(Request $request, $employee_id) { $notifications = DB::table('notifications') ->join('users', 'notifications.notifiable_id' ,'users.id') ->where('users.employee_id', '=', $employee_id) ->limit(5) ->get(); return response()->json($notifications); } public function getNotificationForBell($employee_id) { $user = Auth::user(); $notifications = DB::table('notifications') ->join('users', 'notifications.notifiable_id', '=', 'users.id') ->leftJoin('employee_profile_pictures', function($join) { $join->on('employee_profile_pictures.employee_id', '=', DB::raw('JSON_UNQUOTE(JSON_EXTRACT(notifications.data, "$.created_by"))')); }) ->select('notifications.*', 'employee_profile_pictures.attachment') ->where('users.employee_id', '=', $employee_id) ->where('notifications.notifiable_id', '=', $user->id) ->orderBy('notifications.created_at', 'DESC') ->limit(7) ->get(); // Use the collection's map method to update the attachment URL $notifications = $notifications->map(function ($notification) { if ($notification->attachment) { $notification->attachment = asset('storage/employee-profile/' . $notification->attachment); } return $notification; }); $unreadCount = DB::table('notifications') ->join('users', 'notifications.notifiable_id', 'users.id') ->where('users.employee_id', '=', $employee_id) ->where('notifications.notifiable_id', '=', $user->id) ->whereNull('read_at') ->count(); return response()->json([ 'notifications' => $notifications, 'unread_count' => $unreadCount ]); } public function update(string $id) { DB::connection()->beginTransaction(); try { // Retrieve the first matching notification or throw an exception if not found $notification = DB::table('notifications')->where('id', '=', $id)->first(); if (!$notification) { throw new ModelNotFoundException("Notification not found."); } // Update the read_at field DB::table('notifications')->where('id', '=', $id)->update(['read_at' => now()]); DB::connection()->commit(); return response()->json([ 'status' => true, 'message' => 'Saved successfully!', 'data' => $notification ], 201); } catch (Throwable $e) { DB::connection()->rollback(); return response()->json([ 'status' => false, 'message' => 'Unable to process request. Please try again.', 'data' => $e->getMessage() ]); } } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0.03 |
proxy
|
phpinfo
|
Settings